package com.example.xloong.mymvvm.http;

import com.example.xloong.mymvvm.bean.NewsBean;
import com.example.xloong.mymvvm.retrofit.RetrofitInterface;
import com.example.xloong.mymvvm.utiles.IntentUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Xloong on 2018/1/25.
 */

public class HttpUtils {

    private static final int DEFAULT_TIMEOUT= 8 ; // 连接超时的时间 ，单位：秒
    private static final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(DEFAULT_TIMEOUT,TimeUnit.SECONDS)
            .writeTimeout(DEFAULT_TIMEOUT,TimeUnit.SECONDS).build();
    private static HttpUtils httpUtils;
    private static Retrofit retrofit;
    private static RetrofitInterface mRetrofitInterface;

    private synchronized static RetrofitInterface getRetrofit(){
        //初始化Retrofit的配置
        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(IntentUtil.URL_BASE)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            mRetrofitInterface = retrofit.create(RetrofitInterface.class);
        }
        return mRetrofitInterface;
    }


    //获取推荐数据
    public static Observable<NewsBean> getNewsData(String page, String id) {
        return getRetrofit().getNewsData(page,id);
    }

}
